/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 */
package vn.smartware.co.java.net.commons;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasypt.util.text.BasicTextEncryptor;

/**
 * common util
 * 
 * @author thuctt
 */
public class EncryptionUtils implements Serializable { 

	/**
	 * PASSWORD_KEY = "PASSWORD"
	 */
	public static final String PASSWORD_KEY = "PASSWORD";
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -2700668566545024739L;

	private static Log log = LogFactory.getLog(EncryptionUtils.class);
	
	private BasicTextEncryptor encryptor;
	
	private static EncryptionUtils encryptionUtils;
	
	/**
	 * Constructor
	 *
	 */
	public EncryptionUtils(){
		encryptor = new BasicTextEncryptor();
		encryptor.setPassword(PASSWORD_KEY);
	}
	
	/**
	 * getInstance
	 * @return EncryptionUtils
	 */
	public static EncryptionUtils getInstance(){
		
		if (encryptionUtils == null){
			encryptionUtils = new EncryptionUtils();
		}
		return encryptionUtils;
	}
	
	/**
	 * encrypt Password
	 * @param text
	 * @return String
	 */
	public String encrypt(String text){
		
		String ret = encryptor.encrypt(text);
		log.debug("Encrypted text = " + ret);
		return ret;
	}
	
	/**
	 * check encrypted text
	 * @param iStr
	 * @param encryptStr
	 * @return true/false
	 */
	public boolean checkEncryptedText(String iStr, String encryptStr){
		
		boolean valid = false;
		if (encryptStr.equals(encryptor.encrypt(iStr))){
			valid = true;
		}
		return valid;
	}
	
	/**
	 * decrypt text
	 * @param encryptText
	 * @return String
	 */
	public String decrypt(String encryptText){
		
		String ret = encryptor.decrypt(encryptText);
		log.debug("Decrypted text = " + ret);
		return ret;
	}
}
